home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 3006 / 3006.xpi / components / dhMediaListMgr.js < prev    next >
Text File  |  2010-01-15  |  11KB  |  334 lines

  1. /******************************************************************************
  2.  *            Copyright (c) 2006-2009 Michel Gutierrez. All Rights Reserved.
  3.  ******************************************************************************/
  4.  
  5. /**
  6.  * Constants.
  7.  */
  8.  
  9. const NS_DH_MEDIA_LIST_MGR_CID = Components.ID("{55f8f6d3-9ac7-4046-b1bb-7c732e27d2d6}");
  10. const NS_DH_MEDIA_LIST_MGR_PROG_ID = "@downloadhelper.net/media-list-manager";
  11. const DHNS = "http://downloadhelper.net/1.0#";
  12.  
  13. var Util=null;
  14.  
  15. /**
  16. * Object constructor
  17. */
  18. function DhMediaListMgr() {
  19.     try {
  20.         //dump("[DhMediaListMgr] constructor\n");
  21.         
  22.         this.timer=null;
  23.         this.needSaving=false;
  24.         this.currentURLs=[];
  25.         var prefService=Components.classes["@mozilla.org/preferences-service;1"]
  26.             .getService(Components.interfaces.nsIPrefService);
  27.         this.pref=prefService.getBranch("dwhelper.");
  28.  
  29.         this.storageFile = Components.classes["@mozilla.org/file/directory_service;1"]
  30.             .getService(Components.interfaces.nsIProperties)
  31.             .get("ProfD", Components.interfaces.nsIFile);
  32.         this.storageFile.append("dh-media-lists.rdf");
  33.         if(this.storageFile.exists() && this.pref.getBoolPref("history-enabled")) {
  34.             this.dataSource=Util.getDatasourceFromRDFFile(this.storageFile);
  35.         } else {
  36.             this.dataSource=Components.classes
  37.                   ['@mozilla.org/rdf/datasource;1?name=in-memory-datasource'].
  38.                       createInstance(Components.interfaces.nsIRDFDataSource);
  39.             var hist=Util.createNodeSS(this.dataSource,"urn:root",DHNS+"history-list");
  40.             Util.setPropertyValueRS(this.dataSource,hist,DHNS+"name",Util.getText("medialist.history"));
  41.             this.needSaving=true;
  42.         }
  43.  
  44.         this.observerService =
  45.             Components.classes["@mozilla.org/observer-service;1"]
  46.                 .getService(Components.interfaces.nsIObserverService);
  47.         this.observerService.addObserver(this,"quit-application",false);
  48.         
  49.     } catch(e) {
  50.         dump("!!! [DhMediaListMgr] constructor: "+e+"\n");
  51.     }
  52. }
  53.  
  54. DhMediaListMgr.prototype = {}
  55.  
  56. DhMediaListMgr.prototype.initTimer=function() {
  57.     //dump("[DhMediaListMgr] initTimer()\n");
  58.     if(this.timer==null) {
  59.         this.timer=Components.classes['@mozilla.org/timer;1'].
  60.                  createInstance(Components.interfaces.nsITimer);
  61.         this.timer.init(this,60*1000,Components.interfaces.nsITimer.TYPE_REPEATING_SLACK);
  62.     }
  63. }
  64.  
  65. DhMediaListMgr.prototype.getDataSource=function() {
  66.     return this.dataSource;
  67. }
  68.  
  69. DhMediaListMgr.prototype.addToList=function(list,url,type,pageurl,filename,referer) {
  70.     //dump("[DhMediaListMgr] addToList("+list+","+url+","+type+","+pageurl+","+filename+","+referer+")\n");
  71.     if(!this.pref.getBoolPref("history-enabled"))
  72.         return null;
  73.     try {
  74.     if(Util.getPropertyValueSS(this.dataSource,list,DHNS+"name")==null) {
  75.         dump("!!! [DhMediaListMgr] addToList(): list "+list+" does not exist\n");
  76.         return null;
  77.     }
  78.     var entries=Util.getChildResourcesS(this.dataSource,list,{});
  79.     for(var i=0;i<entries.length;i++) {
  80.         if(Util.getPropertyValueRS(this.dataSource,entries[i],DHNS+"url")==url) {
  81.             //dump("[DhMediaListMgr] !!! addToList(): removing previous entry\n");
  82.             Util.removeReference(this.dataSource,entries[i]);
  83.         }
  84.     }
  85.     this.needSaving=true;
  86.     var entry=Util.createAnonymousNodeS(this.dataSource,list);
  87.     Util.setPropertyValueRS(this.dataSource,entry,DHNS+"url",url);
  88.     if(type!=null)
  89.         Util.setPropertyValueRS(this.dataSource,entry,DHNS+"type",type);
  90.     if(pageurl!=null)
  91.         Util.setPropertyValueRS(this.dataSource,entry,DHNS+"pageurl",pageurl);
  92.     var date=new Date();
  93.     Util.setPropertyValueRS(this.dataSource,entry,DHNS+"date",""+date.getTime());
  94.     Util.setPropertyValueRS(this.dataSource,entry,DHNS+"datestr",""+date.toLocaleString());
  95.     Util.setPropertyValueRS(this.dataSource,entry,DHNS+"filename",""+filename);
  96.     if(referer!=null) {
  97.         Util.setPropertyValueRS(this.dataSource,entry,DHNS+"referer",""+referer);
  98.     }
  99.     
  100.     return entry.Value;
  101.     } catch(e) {
  102.         dump("!!! [DhMediaListMgr] addToList(): "+e+"\n");
  103.         return null;
  104.     }
  105. }
  106.  
  107. DhMediaListMgr.prototype.removeFromList=function(list,media) {
  108.     try {
  109.     if(Util.getPropertyValueSS(this.dataSource,list,DHNS+"name")==null) {
  110.         dump("!!! [DhMediaListMgr] removeFromList(): list "+list+" does not exist\n");
  111.         return null;
  112.     }
  113.     Util.removeReferenceS(this.dataSource,media);
  114.     this.needSaving=true;
  115.     } catch(e) {
  116.         dump("!!! [DhMediaListMgr] removeFromList(): "+e+"\n");
  117.     }
  118. }
  119.  
  120. DhMediaListMgr.prototype.saveToFile=function() {
  121.     //dump("[DhMediaListMgr] saveToFile\n");
  122.     try {
  123.         var serializer="@mozilla.org/rdf/xml-serializer;1";
  124.         var s=Components.classes[serializer].createInstance(Components.interfaces.nsIRDFXMLSerializer);
  125.         s.init(this.dataSource);
  126.         var stream = Components.classes['@mozilla.org/network/file-output-stream;1']
  127.             .createInstance(Components.interfaces.nsIFileOutputStream);
  128.         stream.init(this.storageFile, 42, 0644, 0); 
  129.     
  130.         s.QueryInterface(Components.interfaces.nsIRDFXMLSource).Serialize(stream);
  131.         stream.close();
  132.     } catch(e) {
  133.         dump("!!! [DhMediaListMgr] saveToFile: "+e+"\n");
  134.     }
  135. }
  136.  
  137. DhMediaListMgr.prototype.checkSave=function() {
  138.     //dump("[DhMediaListMgr] checkSave\n");
  139.     if(this.needSaving) {
  140.         this.needSaving=false;
  141.         this.saveToFile();
  142.     }
  143. }
  144.  
  145. DhMediaListMgr.prototype.cleanupCurrentURL = function() {
  146.     var d=new Date().getTime()/1000;
  147.     for(var i=this.currentURLs.length-1;i>=0;i--) {
  148.         var entry=this.currentURLs[i];
  149.         if(d>entry.expire) {
  150.             //dump("[DhMediaListMgr] cleanupCurrentURL("+entry.url+")\n");
  151.             this.currentURLs.splice(i,1);
  152.         }
  153.     }
  154. }
  155.  
  156. DhMediaListMgr.prototype.addCurrentURL = function(item) {
  157.     var expire=new Date().getTime/1000+60;
  158.     for(var i=0;i<this.currentURLs.length;i++) {
  159.         var entry=this.currentURLs[i];
  160.         if(entry.url==item) {
  161.             //dump("[DhMediaListMgr] addCurrentURL("+entry.url+") updated\n");
  162.             entry.expire=expire;
  163.             return;
  164.         }
  165.     }
  166.     //dump("[DhMediaListMgr] addCurrentURL("+item+") added\n");
  167.     this.currentURLs.push({
  168.         url: item,
  169.         expire: expire
  170.     });
  171. }
  172.  
  173. DhMediaListMgr.prototype.removeCurrentURL = function(item) {
  174.     for(var i=0;i<this.currentURLs.length;i++) {
  175.         var entry=this.currentURLs[i];
  176.         if(entry.url==item) {
  177.             this.currentURLs.splice(i,1);
  178.             //dump("[DhMediaListMgr] removeCurrentURL("+entry.url+") removed\n");
  179.             return;
  180.         }
  181.     }
  182. }
  183.  
  184. DhMediaListMgr.prototype.checkCurrentURL = function(item) {
  185.     this.cleanupCurrentURL();
  186.     for(var i=0;i<this.currentURLs.length;i++) {
  187.         var entry=this.currentURLs[i];
  188.         if(entry.url==item) {
  189.             //dump("[DhMediaListMgr] checkCurrentURL("+entry.url+") found\n");
  190.             return true;
  191.         }
  192.     }    
  193.     //dump("[DhMediaListMgr] checkCurrentURL("+item+") (out of "+this.currentURLs.length+") not found\n");
  194.     return false;
  195. }
  196.  
  197. DhMediaListMgr.prototype.observe=function(subject,topic,data) {
  198.     //dump("[DhMediaListMgr] observe("+subject+","+topic+","+data+")\n");
  199.     if(topic=="timer-callback") {
  200.         this.checkSave();
  201.     } else if(topic=="quit-application") {
  202.         this.checkSave();
  203.         var coe=true;
  204.         try {
  205.             coe=this.pref.getBoolPref("history-clearonexit");
  206.         } catch(e) {}
  207.         if(coe)
  208.             this.clearHistory();
  209.         this.observerService.removeObserver(this,"quit-application");
  210.     }
  211. }
  212.  
  213. DhMediaListMgr.prototype.clearHistory = function() {
  214.     try {
  215.         var hEntries=Util.getChildResourcesS(this.dataSource,DHNS+"history-list",{});
  216.         for(var i in hEntries) {
  217.             var entry=hEntries[i];
  218.             Util.removeChildSR(this.dataSource,DHNS+"history-list",entry);
  219.             Util.removeReference(this.dataSource,entry);
  220.         }
  221.         this.saveToFile();
  222.     } catch(e) {
  223.         dump("!!! [DhMediaListMgr] clearHistory: "+e+"\n");
  224.     }
  225. }
  226.  
  227.  
  228. DhMediaListMgr.prototype.QueryInterface = function(iid) {
  229.     //dump("[DhMediaListMgr] QueryInterface("+iid+")\n");
  230.     if(
  231.         iid.equals(Components.interfaces.nsITimerCallback)==false &&
  232.         iid.equals(Components.interfaces.nsIObserver)==false &&
  233.         iid.equals(Components.interfaces.dhIMediaListMgr)==false &&
  234.         iid.equals(Components.interfaces.nsISupports)==false
  235.     ) {
  236.             throw Components.results.NS_ERROR_NO_INTERFACE;
  237.         }
  238.     return this;
  239. }
  240.  
  241.  
  242. var vDhMediaListMgrModule = {
  243.     firstTime: true,
  244.     
  245.     /*
  246.      * RegisterSelf is called at registration time (component installation
  247.      * or the only-until-release startup autoregistration) and is responsible
  248.      * for notifying the component manager of all components implemented in
  249.      * this module.  The fileSpec, location and type parameters are mostly
  250.      * opaque, and should be passed on to the registerComponent call
  251.      * unmolested.
  252.      */
  253.     registerSelf: function (compMgr, fileSpec, location, type) {
  254.  
  255.         if (this.firstTime) {
  256.             this.firstTime = false;
  257.             throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  258.         }
  259.         //dump("*** Registering DhMediaListMgr\n");
  260.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  261.         compMgr.registerFactoryLocation(NS_DH_MEDIA_LIST_MGR_CID,
  262.                                         "DhMediaListMgr",
  263.                                         NS_DH_MEDIA_LIST_MGR_PROG_ID, 
  264.                                         fileSpec,
  265.                                         location,
  266.                                         type);
  267.         //dump("*** Registered DhMediaListMgr\n");
  268.     },
  269.  
  270.     unregisterSelf: function(compMgr, fileSpec, location) {
  271.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  272.         compMgr.unregisterFactoryLocation(NS_DH_MEDIA_LIST_MGR_CID, fileSpec);
  273.     },
  274.  
  275.     /*
  276.      * The GetClassObject method is responsible for producing Factory and
  277.      * SingletonFactory objects (the latter are specialized for services).
  278.      */
  279.     getClassObject: function (compMgr, cid, iid) {
  280.         if (!cid.equals(NS_DH_MEDIA_LIST_MGR_CID)) {
  281.             throw Components.results.NS_ERROR_NO_INTERFACE;
  282.         }
  283.  
  284.         if (!iid.equals(Components.interfaces.nsIFactory)) {
  285.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  286.         }
  287.  
  288.         return this.vDhMediaListMgrFactory;
  289.     },
  290.  
  291.     /* factory object */
  292.     vDhMediaListMgrFactory: {
  293.         /*
  294.          * Construct an instance of the interface specified by iid, possibly
  295.          * aggregating it with the provided outer.  (If you don't know what
  296.          * aggregation is all about, you don't need to.  It reduces even the
  297.          * mightiest of XPCOM warriors to snivelling cowards.)
  298.          */
  299.         createInstance: function (outer, iid) {
  300.             if (outer != null) {
  301.                 throw Components.results.NS_ERROR_NO_AGGREGATION;
  302.             }
  303.     
  304.             //dump("DhMediaListMgr: create instance\n");
  305.  
  306.             if(Util==null) 
  307.                 Util=Components.classes["@downloadhelper.net/util-service;1"]
  308.                     .getService(Components.interfaces.dhIUtilService);
  309.  
  310.             return new DhMediaListMgr().QueryInterface(iid);
  311.         }
  312.     },
  313.  
  314.     /*
  315.      * The canUnload method signals that the component is about to be unloaded.
  316.      * C++ components can return false to indicate that they don't wish to be
  317.      * unloaded, but the return value from JS components' canUnload is ignored:
  318.      * mark-and-sweep will keep everything around until it's no longer in use,
  319.      * making unconditional ``unload'' safe.
  320.      *
  321.      * You still need to provide a (likely useless) canUnload method, though:
  322.      * it's part of the nsIModule interface contract, and the JS loader _will_
  323.      * call it.
  324.      */
  325.     canUnload: function(compMgr) {
  326.         return true;
  327.     }
  328. };
  329.  
  330. function NSGetModule(compMgr, fileSpec) {
  331.     return vDhMediaListMgrModule;
  332. }
  333.  
  334.